<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="query"
        inline size="mini"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-form-item label="报警类型:">
          <el-input
              clearable
              v-model="query.name"
              placeholder="请输入报警类型"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload"
          >查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="toolbar-box" style="margin-bottom:4px; display: flex; justify-content: flex-end">
        <div class="tool-tab">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="handleAdd"
          >新增</el-button>
        </div>
      </div>
      <!-- 数据表格 -->
      <el-table
        border
        lazy
        ref="table"
        :data="data"
        v-loading="loading"
        height="calc(100vh - 360px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column label="序号" width="55" align="center" type="index">
          <template slot-scope="scope">
            <span
              v-text="$setTableIndex(query.page, query.limit, scope.$index)"
            >
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="报警类型名称"
          align="center"
          prop="name"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="排序" width="80" align="center" prop="sort">
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" align="center">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          :resizable="false"
        >
          <template slot-scope="{ row }">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-edit"
              :underline="false"
              @click="handleEdit(row)"
              >修改</el-button
            >
            <el-popconfirm
              title="确定要删除该型号吗？"
              @confirm="remove(row)"
              class="ele-action"
            >
              <el-button
                size="small"
                type="danger"
                slot="reference"
                icon="el-icon-delete"
                :underline="false"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getData"
      />
    </el-card>
    <el-dialog
      :title="title"
      width="600px"
      append-to-body
      @closed="cancel"
      :visible.sync="showEdit"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort" placeholder="请输入排序字段" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "DeviceAlarmType",
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      loading: false, // 表格加载状态
      ids: [], // 选中数组
      selection: [], // 表格选中数据
      total: 0, // 数据总数
      data: [], // 表格数据接口
      // 表格搜索条件
      query: {
        page: 1,
        limit: 10
      },
      showEdit: false, // 是否显示编辑弹窗
      title: "",
      form: {}, //  新增编辑表单// 表单校验
      rules: {
        name: [{ required: true, message: "类型名称不能为空", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    /* 获取表格数据 */
    getData() {
      this.loading = true;
      var name = this.query.name;
      if (!name) {
        name = "";
      }
      this.$http
        .get("/device_alarm_type/index", { params: this.query })
        .then(res => {
          this.loading = false;
          this.total = res.data.count;
          this.data = res.data.data;
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 查询数据 */
    reload() {
      this.getData();
    },
    /* 重置搜索 */
    reset() {
      this.query = {
        page: 1,
        limit: 10
      };
      this.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.selection = selection;
    },
    /* 删除 */
    remove(row) {
      this.loading = true;
      const params = { id: row.id };
      this.$http
        .get("/device_alarm_type/delete", { params: params })
        .then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.getData();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的型号吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          this.$http
            .post("", { id: this.ids })
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.getData();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 新增数据 */
    handleAdd() {
      this.showEdit = true;
      this.title = "新增报警类型";
      this.form = {};
    },
    /* 修改数据 */
    handleEdit(row) {
      const id = row.id;
      this.showEdit = true;
      this.title = "修改报警类型";
      this.form = {};
      this.$http
        .get("/device_alarm_type/info?id=" + id)
        .then(res => {
          console.log(res);
          this.form = res.data.data;
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 取消新增修改 */
    cancel() {
      this.showEdit = false;
      this.form = {};
    },
    /* 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$http
            .post("/device_alarm_type/edit", this.form)
            .then(res => {
              console.log(res);
              this.$message.success("操作成功");
              this.cancel();
              this.getData();
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        }
      });
    }
  }
};
</script>

<style scoped></style>
